const app = getApp()
Page({
  data: {
    listname:[
      {name:'全市'},
      {name:'折扣'},
      {name:'距离'},
      {name:'核销量'}
    ],
    list:[],
    page:1,
    limit:10,
    isSelectsort: 'asc',
    loation:{
        latitude:"",//纬度
        longitude:"" //经度
    },
    nodataShow:false,
    nodata:'',
    listHeight:0,// scroll-wrap 的高度，这个高度是固定的
    triggered: true,
  },

  /*** 生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this
    console.log(that.data)
    wx.createSelectorQuery().select('#header').boundingClientRect(function(rect){
      wx.createSelectorQuery().select('#listTitle').boundingClientRect(function(r){
        that.setData({
          listHeight:wx.getSystemInfoSync().windowHeight - rect.height -r.height-20  // 节点的高度
        })   
      }).exec()   
    }).exec()
    
    wx.getSystemInfoSync().windowHeight
    // 获取地理位置
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        that.data.loation.latitude = res.latitude
        that.data.loation.longitude = res.longitude
        that.getList(
          that,
          that.options.cateid,
          that.data.page,
          that.data.limit,
          that.data.isSelectsort,
          JSON.stringify(that.data.loation)
          )  
      }
    })
  },
  /*** 生命周期函数--监听页面初次渲染完成*/
  onReady: function () {
    
  },
  /*** 生命周期函数--监听页面显示*/
  onShow: function () {},
  /*** 生命周期函数--监听页面隐藏*/
  onHide: function () {},
  /*** 生命周期函数--监听页面卸载*/
  onUnload: function () {},
  /*** 页面相关事件处理函数--监听用户下拉动作*/
  onPullDownRefresh: function () {
    // wx.startPullDownRefresh()
    // wx.stopPullDownRefresh()
  },
  /*** 页面上拉触底事件的处理函数*/
  onReachBottom: function () {},
  /*** 用户点击右上角分享*/
  onShareAppMessage: function () {},
  onRefresh() {
    let that = this
    if (this._freshing) return
    this._freshing = true
    setTimeout(() => {
      this.setData({
        triggered: false,
      })
      this._freshing = false
    }, 3000)
    that.setData({
      page:1
    })
    that.getList(
      that,
      that.options.cateid,
      that.data.page,
      that.data.limit,
      that.data.isSelectsort,
      JSON.stringify(that.data.loation)
      )  
  },
  onScrollLower(){
    let that = this
    if(!that.data.nodataShow){
      that.data.page ++
      that.getList(
        that,
        that.options.cateid,
        that.data.page,
        that.data.limit,
        that.data.isSelectsort,
        JSON.stringify(that.data.loation)
      )  
    }
    
  },
  getList:function(that,cateid,page,limit,value,data,areaid,attr){
    wx.showNavigationBarLoading()              
    wx.showLoading({                       
        title: '加载中',
    })
    wx.request({
      url: 'https://carplus.qingmengcar.com/user/merchant/merchantlists',
      method:"POST",
      data:{
        cate_id:1,
        page:page,
        limit:limit,
        value:value,//排序值->正序倒序值
        data:data,//附加值->经纬度
        area_id:areaid,
        attr:attr,//排序分类值
      },
      success:function(d){
        wx.hideNavigationBarLoading() 
        wx.hideLoading() 
        if(d.data.code == 0){
          var listItm = that.data.list 
          for(let i = 0 ;i<d.data.data.length;i++){
            if(d.data.data[i].title_img){
              d.data.data[i].title_img = 'https://carplus.qingmengcar.com'+d.data.data[i].title_img.split(',')[0]
            } 
          }                                
          if(that.data.page == 1){
              listItm = []
              that.setData({
               list:d.data.data,
              })
          }else{
            that.setData({
              list:listItm.concat(d.data.data)
            })                                         
          }   
        }else{
          that.setData({
            nodataShow:true,
            nodata:d.data.msg
          })            
        }
      },
      fail:function(d){
        wx.hideNavigationBarLoading()
        wx.hideLoading()
      }
    })
  }
})